<?php
/**
 * 运营-运营总表
 * (model)
 */
namespace App\Models\Operation;

use App\Models\Basics\SysCountry;
use App\Models\Basics\SysArea;
use App\Util\User;
use App\Models\Product\ProductClassify;
use Illuminate\Database\Eloquent\Model;
class BATotal extends Model
{
    protected $table = 'operation_total';

    protected $guarded = [];

    public function area(){
        return $this->belongsTo(SysArea::class, 'area_id');
    }

    public function country(){
        return $this->belongsTo(SysCountry::class, 'country_id');
    }

    public function classify(){
        return $this->belongsTo(ProductClassify::class, 'classify_id');
    }

    public static function headersField(){
        $fields = [];
        $fields['report_date'] = ['title' => '日期','sumField' => false];
        $fields['area_id'] = ['title' => '站点','sumField' => false];
        $fields['country_id'] = ['title' => '国家','sumField' => false];
        $fields['seller_id'] = ['title' => '销售','sumField' => false];
        $fields['classify_id'] = ['title' => '品类','sumField' => false];
        $fields['season'] = ['title' => '季节性','sumField' => false];
        $fields['profit_rate'] = ['title' => '利润率范围','sumField' => false];
        $fields['star'] = ['title' => '星级','sumField' => false];
        $fields['ratings'] = ['title' => 'Ratings','sumField' => false];
        $fields['b_rank'] = ['title' => '大类排名','sumField' => false];
        $fields['s_rank'] = ['title' => '小类排名','sumField' => false];
        $fields['sessions_app'] = ['title' => 'SessionsApp','sumField' => true];
        $fields['sessions_browser'] = ['title' => 'SessionsBrowser','sumField' => true];
        $fields['sessions_total'] = ['title' => 'SessionsTotal','sumField' => true];
        $fields['ordered'] = ['title' => '总订单','sumField' => true];
        $fields['ordered_r'] = ['title' => '自然订单','sumField' => true];
        $fields['ordered_sales'] = ['title' => '销售额','sumField' => true];
//        $fields['os_app'] = ['title' => '转换率App','sumField' => false];
        $fields['os_browser'] = ['title' => '转换率Browser','sumField' => false];
        $fields['os_total'] = ['title' => '转换率Total','sumField' => false];
        $fields['page_views_app'] = ['title' => 'PageViewsApp','sumField' => true];
        $fields['page_views_browser'] = ['title' => 'PageViewsBrowser','sumField' => true];
        $fields['page_views_total'] = ['title' => 'PageViewsTotal','sumField' => true];
        $fields['impressions'] = ['title' => 'Impressions','sumField' => true];
        $fields['clicks'] = ['title' => 'Clicks','sumField' => true];
        $fields['ctr'] = ['title' => 'CTR','sumField' => false];
        $fields['spend'] = ['title' => '广告费用','sumField' => true];
        $fields['total_units'] = ['title' => '广告订单','sumField' => true];
        $fields['cpc'] = ['title' => '广告CPC','sumField' => false];
        $fields['tc'] = ['title' => '广告转换率','sumField' => false];
        $fields['total_sales'] = ['title' => '广告销售额','sumField' => true];
        $fields['acos'] = ['title' => '广告acos','sumField' => false];
        $fields['spend_os'] = ['title' => '广告费占比销售额','sumField' => false];
        $fields['to'] = ['title' => '广告订单占比','sumField' => false];
        $fields['promotion_discount'] = ['title' => '促销费','sumField' => true];
        $fields['reimburse'] = ['title' => '刷单费用','sumField' => true];
        $fields['profit'] = ['title' => '毛利','sumField' => true];
        $fields['pos'] = ['title' => '毛利率','sumField' => false];
        return $fields;
    }

    public static function getDownloadData($re){
        foreach ($re as $row){
            $temp = [];
            $temp['report_date'] = $row->report_date;
            $temp['area_id'] = $row->area->area ?? '';
            $temp['country_id'] = $row->country->country ?? '';
            $temp['seller_id'] = User::getUsernameById($row->seller_id);
            $temp['classify_id'] = $row->classify->title ?? '';
            $temp['season'] = ProductClassify::$_season[$row->classify->season ?? 3];
            $temp['profit_rate'] = $row->profit_rate;
            $temp['star'] = $row->star;
            $temp['ratings'] = $row->ratings;
            $temp['b_rank'] = $row->b_rank;
            $temp['s_rank'] = $row->s_rank;
            $temp['sessions_app'] = $row->sessions_app;
            $temp['sessions_browser'] = $row->sessions_browser;
            $temp['sessions_total'] = $row->sessions_total;
            $temp['ordered'] = $row->ordered;
            $temp['ordered_r'] = $row->ordered_r;
            $temp['ordered_sales'] = $row->ordered_sales;
//            $temp['os_app'] = intval($row->sessions_app == 0) ? '' : round($row->ordered / $row->sessions_app,3) * 100 .'%';
            $temp['os_browser'] = intval($row->sessions_browser == 0) ? '' : round($row->ordered / $row->sessions_browser,3) * 100 .'%';
            $temp['os_total'] = intval($row->sessions_total == 0) ? '' : round($row->ordered / $row->sessions_total,3) * 100 .'%';
            $temp['page_views_app'] = $row->page_views_app;
            $temp['page_views_browser'] = $row->page_views_browser;
            $temp['page_views_total'] = $row->page_views_total;
            $temp['impressions'] = $row->impressions;
            $temp['clicks'] = $row->clicks;
            $temp['ctr'] = intval($row->impressions == 0) ? '' : round($row->clicks / $row->impressions,3) * 100 .'%';
            $temp['spend'] = $row->spend;
            $temp['total_units'] = $row->total_units;
            $temp['cpc'] = intval($row->clicks == 0) ? '' : round($row->spend / $row->clicks,3);
            $temp['tc'] = intval($row->clicks == 0) ? '' : round($row->total_units / $row->clicks,3) * 100 .'%';
            $temp['total_sales'] = $row->total_sales;
            $temp['acos'] = floatval($row->total_sales == 0) ? '' : round($row->spend / $row->total_sales,3) * 100 .'%';
            $temp['spend_os'] = floatval($row->spend_os == 0) ? '' : $row->spend_os .'%';
            $temp['to'] = intval($row->ordered == 0) ? '' : round($row->total_units / $row->ordered,3) * 100 .'%';
            $temp['promotion_discount'] = $row->promotion_discount;
            $temp['reimburse'] = $row->reimburse;
            $temp['profit'] = $row->profit;
            $temp['pos'] = floatval($row->ordered_sales == 0) ? '' : round($row->profit / $row->ordered_sales,3) * 100 .'%';
            yield $temp;
        }
    }
}
